package com.example.weiwing.home.study;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.weiwing.R;
import com.example.weiwing.config.WeiWingConfig;
import com.example.weiwing.entity.Course;
import com.example.weiwing.home.HomeActivity;
import com.example.weiwing.util.TimeUtils;
import com.squareup.picasso.Picasso;


import java.text.DateFormat;
import java.util.List;

public class CourseShowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final List<Course> courses;
    private final Context context;

    public CourseShowAdapter(Context context, List<Course> data) {
        this.context = context;
        courses = data;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_course_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder) holder;
        Course course = courses.get(position);
        Picasso.get().load(WeiWingConfig.getUrl() + course.getImgUrl()).into(((ViewHolder) holder).showImg);
        //Bitmap bimage = getBitmapFromURL(lesson.getImgUrl());
        // vh.showImg.setImageBitmap(bimage);

        vh.title.setText(course.getTitle());
        vh.publishTime.setText(DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.DEFAULT).format(course.getPublishTime()));
        vh.playCount.setText(String.valueOf(course.getVideoView()));
        vh.duration.setText(TimeUtils.sumSecondToTime(course.getTotolTime()));
        vh.author.setText(course.getUser().getNickName());
        vh.playButton.setOnClickListener(view -> {
                Intent intent = new Intent(view.getContext(), CoursePlayer.class);
                intent.putExtra("courseId", String.valueOf(course.getCourseId()));
                view.getContext().startActivity(intent);

        });

    }

    @Override
    public int getItemCount() {
        return courses.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView showImg;
        private ImageView playButton;
        private TextView playCount;
        private TextView duration;

        private TextView title;
        private TextView author;
        private TextView publishTime;

        public ViewHolder(@NonNull View view) {
            super(view);
            showImg = view.findViewById(R.id.studyLessonShowImg);
            playButton = view.findViewById(R.id.studyLessonPlayButton);
            playCount = view.findViewById(R.id.studyLessonPlayCount);
            duration = view.findViewById(R.id.studyLessonDuration);
            title = view.findViewById(R.id.studyLessonTitle);
            author = view.findViewById(R.id.studyLessonAuthor);
            publishTime = view.findViewById(R.id.studyLessonPublishTime);
        }
    }
}
